package com.baizhi.service.impl;

import com.baizhi.annotation.AddCacheAnnotation;
import com.baizhi.entity.PmsCategory;
import com.baizhi.dao.PmsCategoryMapper;
import com.baizhi.service.IPmsCategoryService;
import com.baizhi.vo.CascaderNodeVo;
import com.baizhi.vo.CategoryVo;
import com.baizhi.vo.ZtreeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.aspectj.lang.annotation.Around;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品三级分类 服务实现类
 * </p>
 *
 * @author xhc
 * @since 2020-09-26
 */
@Service
public class PmsCategoryServiceImpl extends ServiceImpl<PmsCategoryMapper, PmsCategory> implements IPmsCategoryService {


    @Resource
    private PmsCategoryMapper categoryMapper;

    @Override
    public List<CategoryVo> selectCategorysByStream() {
        List<PmsCategory> pmsCategories = categoryMapper.selectList(null);

        /**
         *  三级联查通过stream实现
         * 1 先找的所有的一级菜单
         * 2 根据一级菜单找到所有的二级惨淡
         * 3 根据二级菜单找到所有的三级菜单
         *
         */
        List<CategoryVo> categoryVoList = pmsCategories.stream().filter(pmsCategory ->
                pmsCategory.getParentCid() == 0)
                .map(pmsCategory -> {

//                    对象转化   -----vo
                    CategoryVo categoryVo = new CategoryVo();
                    categoryVo.setId(pmsCategory.getCatId());
                    categoryVo.setTitle(pmsCategory.getName());
                    categoryVo.setParentCid(pmsCategory.getParentCid());
                    categoryVo.setChildren( getCategoryList(categoryVo, pmsCategories));
                    return categoryVo;
                }).collect(Collectors.toList());

        return categoryVoList;

    }

    @Override
    @AddCacheAnnotation
    public List<ZtreeVo> selectZtreeCategorysByStream() {
        List<PmsCategory> pmsCategories = categoryMapper.selectList(null);
        List<ZtreeVo> collect = pmsCategories.stream().filter(pmsCategory -> pmsCategory.getParentCid() == 0).map(pmsCategory -> {
            ZtreeVo ztreeVo = new ZtreeVo();
            ztreeVo.setId(pmsCategory.getCatId());
            ztreeVo.setName(pmsCategory.getName());
            ztreeVo.setPid(pmsCategory.getParentCid());
            ztreeVo.setChildren(getChildrenNode(ztreeVo, pmsCategories));
            return ztreeVo;
        }).collect(Collectors.toList());
        return collect;
    }


    private List<ZtreeVo> getChildrenNode(ZtreeVo ztreeVo, List<PmsCategory> pmsCategories) {
        List<ZtreeVo> list = pmsCategories.stream().filter(pmsCategory -> pmsCategory.getParentCid()==ztreeVo.getId()
        ).map(pmsCategory -> {
            ZtreeVo ztreeVo1 = new ZtreeVo();
            ztreeVo1.setId(pmsCategory.getCatId());
            ztreeVo1.setName(pmsCategory.getName());
            ztreeVo1.setPid(pmsCategory.getParentCid());
            ztreeVo1.setChildren(getChildrenNode(ztreeVo1, pmsCategories));
            return ztreeVo1;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 级联下啦
     * @return
     */
    @Override
    public List<CascaderNodeVo> getCascaderNodeVos() {
        List<PmsCategory> list = list();
        List<CascaderNodeVo> collect = list.stream().filter(pmsCategory -> pmsCategory.getParentCid() == 0).map(pmsCategory -> {
            CascaderNodeVo vo = new CascaderNodeVo();
            vo.setValue(pmsCategory.getCatId());
            vo.setLabel(pmsCategory.getName());
            vo.setChildren(getCascaderChildrenNode(vo, list));
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    private List<CascaderNodeVo> getCascaderChildrenNode(CascaderNodeVo vo, List<PmsCategory> list) {

        return list().stream().filter(pmsCategory -> vo.getValue()==pmsCategory.getParentCid()).map(pmsCategory ->{
            CascaderNodeVo vo1 = new CascaderNodeVo();
            vo1.setValue(pmsCategory.getCatId());
            vo1.setLabel(pmsCategory.getName());
            vo1.setChildren(getCascaderChildrenNode(vo1, list));
            return vo1;
        } ).collect(Collectors.toList());
    }



    private List<CategoryVo> getCategoryList(CategoryVo categoryVo, List<PmsCategory> pmsCategories) {
        /**
         *
         * 根据bzmenu在在集合中找到下一级
         */
        List<CategoryVo> Vos = pmsCategories.stream().filter(pmsCategory -> {
            return pmsCategory.getParentCid() == categoryVo.getId();
        }).map(pmsCategory -> {
//            对象转化   bzmenu-----vo
            CategoryVo categoryVo1 = new CategoryVo();
            categoryVo1.setId(pmsCategory.getCatId());
            categoryVo1.setParentCid(pmsCategory.getParentCid());
            categoryVo1.setTitle(pmsCategory.getName());
            categoryVo1.setChildren(getCategoryList(categoryVo1, pmsCategories));
            return categoryVo1;
        }).collect(Collectors.toList());

        return Vos;
    }


    public PmsCategory  selectCategoryByName(String  name) {

        QueryWrapper<PmsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        List<PmsCategory> pmsCategories = categoryMapper.selectList(queryWrapper);
        for (PmsCategory category : pmsCategories) {
            if (category.getParentCid() != 0) {
                return category;
            }
        }
        return null;
    }
}
